/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package msgclient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.MouseInputAdapter;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Данила
 */
public class Chat extends javax.swing.JFrame {

    private String[] users = (String[]) MSGClientImpl.server.getListOnlineClients().toArray(new String[MSGClientImpl.server.getListOnlineClients().size()]);
    private String opponent = "";

//    public void updateData(final String username) {
//        try {
//            if (MSGClientImpl.server.sendMessage(username) != null) {
//                genMsg.append(MSGClientImpl.server.sendMessage(username));
//            }
//            if (MSGClientImpl.server.getListOnlineClients().size() != users.length) {
//                users = (String[]) MSGClientImpl.server.getListOnlineClients().toArray(new String[MSGClientImpl.server.getListOnlineClients().size()]);
//                jList1.setModel(new javax.swing.AbstractListModel() {
//                    String[] strings = users;
//
//                    public int getSize() {
//                        return strings.length;
//                    }
//
//                    public Object getElementAt(int i) {
//                        return strings[i];
//                    }
//                });
//            }
//
//        } catch (RemoteException ex) {
//            Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
//        }
//
//
//    }
    public Chat() throws RemoteException {
        initComponents();
    }

    private void initComponents() throws RemoteException {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        genMsg = new JTextArea(30, 43);


        jScrollPane2.setSize(MAXIMIZED_VERT, MAXIMIZED_HORIZ);
        genMsg.setEditable(false);
        genMsg.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N


        jInternalFrame1.setVisible(true);
        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = users;

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });

        jList1.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                jTabbedPane1.addTab(users[e.getFirstIndex()], jScrollPane2);
                opponent = users[e.getFirstIndex()];
            }
        });

        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
                jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE));
        jInternalFrame1Layout.setVerticalGroup(
                jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1));

        jScrollPane2.setViewportView(genMsg);


        jButton1.setText("Send");

        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                genMsg.append("\n" + Login.getUsername() + ": " + jTextField1.getText());
                try {
                    MSGClientImpl.server.receiveMessage(opponent, jTextField1.getText());
                } catch (RemoteException ex) {
                    Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        jButton2.setText("Recieve");

        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String reply = null;
                try {
                    reply = MSGClientImpl.server.sendMessage(Login.getUsername());
                } catch (RemoteException ex) {
                    Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (reply != null) {
                    genMsg.append("\n" + opponent + ": " + reply);
                }

            }
        });

        jButton3.setText("Send File");

        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                jFileChooser1 = new JFileChooser();
                int choice = jFileChooser1.showDialog(null, "Select File");
                if (choice == JFileChooser.APPROVE_OPTION) {
                    try {
                        MSGClientImpl.server.getFileFromClient(FileUtils.readFileToByteArray(jFileChooser1.getSelectedFile()));
                    } catch (IOException ex) {
                        Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

            }
        });

        jButton4.setText("Run Programm");

        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    MSGClientImpl.server.runProgram(jTextField1.getText());
                } catch (RemoteException ex) {
                    Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jButton1)
                .addComponent(jButton2)
                .addComponent(jButton3)
                .addComponent(jButton4)
                .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING))));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1)
                .addComponent(jButton2)
                .addComponent(jButton3)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jInternalFrame1)));

        pack();
    }// </editor-fold>                        
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private JTextArea genMsg;
    private JFileChooser jFileChooser1;
    // End of variables declaration                   
}
